﻿using _01_Tower4._0.GameFrame.Manager;
using _01_Tower4._0.Tool;
using _01_Tower4._0;
using _01_Tower4._0.CharacterBase;
using _01_Tower4._0.Interface;
using _01_Tower4._0.Scene;
using static _01_Tower4._0.GameFrame.Game;
using _01_Tower4._0.Skills.Base;

namespace _01_Tower4._0.GameFrame.System
{
    internal class CombatSystem
    {
        public enum Selected
        {
            Attack,
            Skill,
            Item,
            Escape,
            Max
        }

        // 战斗系统
        // 触发逻辑
        // 遇到敌人 -> 打开系统 -> 传入双方数据
        // 战斗流程
        // 按行动速度发动攻击
        // 按照回合轮流交替
        bool start, turn;
        IAttack attacker, defender;
        Selected selected;
        Random rand;
        bool menuIsOpen;
        int menuX, menuY;
        IScene scene;
        public Map map;
        SkillSystem skill;
        public static bool SkillIsOpen;
        public static bool BagIsOpen;

        public CombatSystem(int x, int y, IScene scene)
        {
            SkillIsOpen = true;
            BagIsOpen = true;

            this.scene = scene;
            start = false;
            turn = true;
            selected = Selected.Attack;
            rand = new Random();
            menuIsOpen = false;
            menuX = x;
            menuY = y;
            attacker = defender = null;
            EventSystem.Instance.AddEvent("战斗结束", () =>
            {
                start = false;
                Character.IsCombating = false;
                Console.Clear();
                SceneManager.lastSceneName = "SceneMap1";
                SceneManager.sceneName = "SceneMap1";
                turn = true;
            });

            string[] paths = { "../../../Map/Slime.csv",
                               "../../../Map/SlimeTakeDamage.csv",
                               "../../../Map/SlimeDead.csv",
                               "../../../Map/Goblin.csv",
                               "../../../Map/MaoHalfBody.csv"};
            map = new Map(paths, 60, 30, 0, 0, scene);
        }

        public void StartCombat(IAttack attacker, IAttack defender, SkillSystem skill)
        {
            start = true;
            SceneManager.sceneName = "SceneCombat";
            if (SceneMap1.csTarget == "哥布林")
            {
                map.ChangePicture(3);
            }
            if (SceneMap1.csTarget == "史莱姆")
            {
                map.ChangePicture(0);
            }
            if (SceneMap1.csTarget == "魔王")
            {
                map.ChangePicture(4);
            }
            Character.IsCombating = true;
            this.attacker = attacker;
            this.defender = defender;
            this.skill = skill;
        }

        public void ShowInfo(IAttack character, int x, int y)
        {
            PrintFrame(8, 14, x, y);
            Utility.MyPrint(((Character)character).Name, x + 1, y + 1);
            Utility.MyPrint("-------", x + 1, y + 2);
            Utility.MyPrint($"Hp : {character.Hp}/{character.MaxHp}", x + 1, y + 3);
            Utility.MyPrint($"Mp : {character.Mp}/{character.MaxMp}", x + 1, y + 4);
            Utility.MyPrint($"Atk: {character.Atk}", x + 1, y + 5);
            Utility.MyPrint($"Def: {character.Def}", x + 1, y + 6);
        }

        public void ShowMenu()
        {
            if (!menuIsOpen) return;

            // 外框
            PrintFrame(8, 23, menuX, menuY, ConsoleColor.White, false);

            // 内框
            PrintFrame(3, 10, menuX + 1, menuY + 1, selected == Selected.Attack ? ConsoleColor.Red : ConsoleColor.White, false);
            Utility.MyPrint("攻击", menuX + 4, menuY + 2, selected == Selected.Attack ? ConsoleColor.Red : ConsoleColor.White);

            PrintFrame(3, 10, menuX + 12, menuY + 1, selected == Selected.Skill ? ConsoleColor.Red : ConsoleColor.White, false);
            Utility.MyPrint("技能", menuX + 15, menuY + 2, selected == Selected.Skill ? ConsoleColor.Red : ConsoleColor.White);

            PrintFrame(3, 10, menuX + 1, menuY + 4, selected == Selected.Item ? ConsoleColor.Red : ConsoleColor.White, false);
            Utility.MyPrint("道具", menuX + 4, menuY + 5, selected == Selected.Item ? ConsoleColor.Red : ConsoleColor.White);

            PrintFrame(3, 10, menuX + 12, menuY + 4, selected == Selected.Escape ? ConsoleColor.Red : ConsoleColor.White, false);
            Utility.MyPrint("逃跑", menuX + 15, menuY + 5, selected == Selected.Escape ? ConsoleColor.Red : ConsoleColor.White);
        }

        void ClearMenu()
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 23; j++)
                {
                    Utility.MyPrint("  ", menuX + j, menuY + i);
                }
            }
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 12; j++)
                {
                    Utility.MyPrint("  ", menuX + 12 + j, menuY - 8 + i);
                    Utility.MyPrint("  ", menuX + j, menuY - 8 + i);
                }
            }
        }

        public void Input()
        {
            // 当菜单是打开状态才能操作
            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.W))
            {
                selected -= 2;  // 每行相差1个列数
                if (selected < 0)
                {
                    selected += 2 * 2; // -1行和最后一行正好相差行数个列数
                }
            }

            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.S))
            {
                selected += 2;  // 每行相差1个列数
                if (selected > Selected.Max - 1)
                {
                    selected -= 2 * 2; // -1行和最后一行正好相差行数个列数
                }
            }

            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.A))
            {
                int line = (int)selected / 2;
                selected--;
                if ((int)selected < line * 2) // 小于当前行最左边的数 即行号*列数
                {
                    selected += 2; // -1列和最后一列正好相差列数
                }
            }

            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.D))
            {
                int line = (int)selected / 2;
                selected++;
                if ((int)selected > (line + 1) * 2 - 1) // 大于当前行最右边的数 即(行号+1)*列数-1
                {
                    selected -= 2; // -1列和最后一列正好相差列数
                }
            }

            if (menuIsOpen && InputSystem.Instance.IsKeyPressed(ConsoleKey.Enter))
            {
                //for (int i = 0; i < skill.skills.Count; i++)
                //{
                //    skill.skills[i].CoolDown();
                //}
                switch (selected)
                {
                    case Selected.Attack:
                        Attack();
                        break;
                    case Selected.Skill:
                        OpenSkill();
                        break;
                    case Selected.Item:
                        OpenBag();
                        break;
                    case Selected.Escape:
                        Escape();
                        break;
                }
            }
        }

        void Attack()
        {
            if (turn)
            {
                attacker.Attack(defender);
                //SkillSystem.cd++;
            }
            else
            {
                defender.Attack(attacker);
                for (int i = 0; i < skill.skills.Count; i++)
                {
                    skill.skills[i].CoolDown();
                }
            }
            // 关闭菜单
            menuIsOpen = false;
            ClearMenu();
            // 切换回合
            turn = !turn;
        }

        public void OpenSkill()
        {
            if (SkillIsOpen)
            {
                SceneManager.lastSceneName = "SceneCombat";
                SceneManager.sceneName = "SceneSkill";
                ClearMenu();
                SkillIsOpen = false;
            }
            if (SkillSystem.IsUse)
            {
                Skill();
                SkillSystem.IsUse = false;
                SkillIsOpen = true;
            }
        }

        void Skill()
        {
            if (turn)
            {
                
                
            }
            else
            {
                defender.Skill(attacker);
                for (int i = 0; i < skill.skills.Count; i++)
                {
                    skill.skills[i].CoolDown();
                }
            }
            menuIsOpen = false;
            ClearMenu();
            turn = !turn;
        }

        public void OpenBag()
        {
            if (BagIsOpen)
            {
                SceneManager.lastSceneName = "SceneCombat";
                SceneManager.sceneName = "SceneBag";
                ClearMenu();
                BagIsOpen = false;
            }
            if (InventorySystem.IsUse)
            {
                Item();
                InventorySystem.IsUse = false;
                BagIsOpen = true;
            }
        }

        void Item()
        {
            if (turn)
            {
                
            }
            else
            {
                defender.Item(attacker);
                for (int i = 0; i < skill.skills.Count; i++)
                {
                    skill.skills[i].CoolDown();
                }
            }
            menuIsOpen = false;
            ClearMenu();
            turn = !turn;
        }

        void Escape()
        {
            if (turn)
            {
                attacker.Escape();
            }
            else
            {
                defender.Escape();
            }
            menuIsOpen = false;
            ClearMenu();
            turn = !turn;
        }

        public void Update()
        {
            // 如果没有开始战斗 就不进行逻辑更新
            if (!start) { return; }

            // 回合切换逻辑
            switch (turn)
            {
                case true:
                    // 进攻方逻辑
                    // 打开菜单 后面再讨论敌人AI 先统一都打开
                    if (!menuIsOpen)
                    {
                        menuIsOpen = true;
                        selected = Selected.Attack;
                    }
                    break;
                case false:
                    int i = rand.Next(4);
                    if(SceneMap1.csTarget == "魔王")
                    {
                        switch (i)
                        {
                            case 0:
                                Attack();
                                break;
                            case 1:
                                Skill();
                                break;
                            case 2:
                                Attack();
                                break;
                            case 3:
                                Skill();
                                break;
                        }
                    }
                    else
                    {
                        switch (i)
                        {
                            case 0:
                                Attack();
                                break;
                            case 1:
                                Skill();
                                break;
                            case 2:
                                Attack();
                                break;
                            case 3:
                                Escape();
                                break;
                        }
                    }
                    break;
            }
            //Thread.Sleep(100);
        }

        public void PlayerInfoRender(IAttack attacker)
        {
            ShowInfo(attacker, menuX+16, menuY - 8);
        }

        public void Render()
        {
            if (!start) return;

            ShowInfo(attacker, menuX, menuY-8);
            ShowInfo(defender, menuX + 14, menuY-8);
            ShowMenu();
        }

        void PrintFrame(int row, int col, int x, int y, ConsoleColor color = ConsoleColor.White, bool clear = true)
        {
            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    string temp = "";
                    if (i == 0 && j == 0)
                    {
                        temp = "┌";
                    }
                    else if (i == 0 && j == col - 1)
                    {
                        temp = "┐";
                    }
                    else if (i == row - 1 && j == 0)
                    {
                        temp = "└";
                    }
                    else if (i == row - 1 && j == col - 1)
                    {
                        temp = "┘";
                    }
                    else if (i == 0 || i == row - 1)
                    {
                        temp = "─";
                    }
                    else if (j == 0 || j == col - 1)
                    {
                        temp = "│";
                    }
                    else if (clear)
                    {
                        temp = "  ";
                    }
                    Utility.MyPrint(temp, x + j, y + i, color);
                }
            }
        }
    }
}
